# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: teacher_view.py
6.2 讲师视图， 讲师可管理自己的课程， 上课时选择班级，
      查看班级学员列表 ， 修改所管理的学员的成绩
Author: kij
Create Date: 2021/2/22
-------------------------------------------------
"""
import hashlib

from interface import teacher_interface, common_interface
from lib import common

logon_state = {"user": None}


def logon():
	print("登录".center(50, "="))
	user_name = input("请输入用户名:").strip()
	user_password = input("请输入用户密码:").strip()
	m = hashlib.md5(user_password.encode('utf-8'))
	m.update(user_password.encode('utf-8'))
	exists, data = common_interface.all_logon(user_name, m.hexdigest(), user_type='teacher')
	if exists:
		print("登录成功")
		logon_state['user'] = user_name
	else:
		print("登录失败")


# 管理自己的课程
@common.logon('teacher')
def all_lesson():
	is_success, all_lessons = teacher_interface.all_lesson_interface(logon_state['user'])
	if is_success:
		for item in all_lessons:
			print(item)
	else:
		print(all_lessons)


# 新增课程
@common.logon('teacher')
def add_lesson():
	# 拿到学校列表
	has_school, school_list = teacher_interface.get_my_school(logon_state['user'])
	if has_school:
		# 选择学校
		for item in range(len(school_list)):
			print("%s %s" % (item, school_list[item].user_name))
		school_num = input("请输入选择的学校:").strip()
		# 拿到学校的课程
		lesson_list = common_interface.get_all_lesson(school_list[int(school_num)].user_name)
		for item in range(len(lesson_list)):
			print("%s %s" % (item, lesson_list[item].user_name))
		lesson_num = input("请输入选择的课程:").strip()
		# 选择课程然后添加
		is_success, my_lesson = teacher_interface.add_lesson_interface(
			logon_state['user'],
			lesson_list[int(lesson_num)].user_name)
		print(my_lesson)
	else:
		print(school_list)


# 查看班级学员列表
@common.logon('teacher')
def student_list():
	# 拿到学校列表
	has_school, school_list = teacher_interface.get_my_school(logon_state['user'])
	if has_school:
		# 选择学校
		for item in range(len(school_list)):
			print("%s %s" % (item, school_list[item].user_name))
		school_num = input("请输入选择的学校:").strip()
		# 拿到学校的课程
		lesson_list = common_interface.get_all_lesson(school_list[int(school_num)].user_name)
		for item in range(len(lesson_list)):
			print("%s %s" % (item, lesson_list[item].user_name))
		lesson_num = input("请输入选择的课程:").strip()
		# 选择课程然后添加
		is_success, all_students = teacher_interface.student_list_interface(
			logon_state['user'],
			lesson_list[int(lesson_num)].user_name)
		if not is_success:
			print(all_students)
		else:
			for item in all_students:
				print(item)
	else:
		print(school_list)


# 修改所管理的学员的成绩
@common.logon('teacher')
def change_student_num():
	# 拿到学校列表
	has_school, school_list = teacher_interface.get_my_school(logon_state['user'])
	if has_school:
		# 选择学校
		for item in range(len(school_list)):
			print("%s %s" % (item, school_list[item].user_name))
		school_num = input("请输入选择的学校:").strip()
		# 拿到学校的课程
		lesson_list = common_interface.get_all_lesson(school_list[int(school_num)].user_name)
		for item in range(len(lesson_list)):
			print("%s %s" % (item, lesson_list[item].user_name))
		lesson_num = input("请输入选择的课程:").strip()
		# 选择课程然后添加
		is_success, all_students = teacher_interface.student_list_interface(
			logon_state['user'],
			lesson_list[int(lesson_num)].user_name)
		if not is_success:
			print(all_students)
		else:
			for item in range(len(all_students)):
				print(item, all_students[item])
			select_student = input('输入学生序号:').strip()
			student_score = input('输入学生分数:').strip()

			teacher_interface.change_student_num_interface(
				logon_state['user'], lesson_list[int(lesson_num)].user_name,
				all_students[int(select_student)], student_score
			)
	else:
		print(school_list)


func_dict = {
	"1": ["登录", logon],
	"2": ["管理课程", all_lesson],
	"3": ["选择班级", add_lesson],
	"4": ["学员列表", student_list],
	"5": ["修改分数", change_student_num],
}


def show_index():
	while True:
		print("老师视图".center(50, "="))
		for item in func_dict:
			print("%s %s".center(50, " ") % (item, func_dict[item][0]))
		choice = input("输入选项:").strip()
		if not choice.isdecimal():
			continue
		if choice not in ["1", "2", "3", "4", "5"]:
			continue

		func_dict[choice][1]()
